package org.to_zero.springboot.jwt.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.to_zero.springboot.jwt.entity.SysUser;
import org.to_zero.springboot.jwt.entity.vo.ResponseEntity;
import org.to_zero.springboot.jwt.service.IAdminService;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminController {

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Resource
    private IAdminService adminService;

    @PostMapping("login")
    public ResponseEntity login(@RequestBody SysUser user) {
        String token = null;
        try {
            token = adminService.login(user);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.error("用户名或密码有误");
        }
        if (StringUtils.hasLength(token)) {
            Map<String,String> data = new HashMap<>();
            data.put("tokenHead",tokenHead);
            data.put("access_token",token);
            return ResponseEntity.success(data);
        }
        return ResponseEntity.error("用户名或密码有误");
    }
}
